import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class BottomNavigationBarPage extends StatelessWidget {
  const BottomNavigationBarPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("BottomNavigationBarPage"),
      ),
      body: ListView(
        children: [
          ListTile(
            title: TextButton(
                onPressed: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) =>
                              const BottomNavigationBarPage1()));
                },
                child: const Text("BottomNavigationBarPage1")),
          ),
          ListTile(
            title: TextButton(
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) =>
                            const BottomNavigationBarPage2()));
              },
              child: const Text("BottomNavigationBarPage2"),
            ),
          )
        ],
      ),
    );
  }
}

//第一个页面
class BottomNavigationBarPage1 extends StatefulWidget {
  const BottomNavigationBarPage1({super.key});

  @override
  State<StatefulWidget> createState() {
    return _BottomNavigationBarPageState1();
  }
}

class _BottomNavigationBarPageState1 extends State<BottomNavigationBarPage1> {
  late int _selectedIndex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  final List<Widget> _widgetOptions = <Widget>[
    const Text(
      'Index 0:Home',
      style: optionStyle,
    ),
    const Text(
      "Index 1: Business",
      style: optionStyle,
    ),
    const Text(
      "Index 2: School",
      style: optionStyle,
    )
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("BottomNavigationBarPage"),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), label: "home"),
          BottomNavigationBarItem(
              icon: Icon(Icons.business_sharp), label: "business"),
          BottomNavigationBarItem(icon: Icon(Icons.school), label: "school"),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}

//第二个页面
class BottomNavigationBarPage2 extends StatefulWidget {
  const BottomNavigationBarPage2({super.key});

  @override
  State<StatefulWidget> createState() {
    return _BottomNavigationBarPage2State();
  }
}

class _BottomNavigationBarPage2State extends State<BottomNavigationBarPage2> {
  int lastTime = 0;
  late int _selectTabIndex = 0;
  final ScrollController _homeController = ScrollController();

  void _showModal() {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            content: const Text("Example Dialog"),
            actions: [
              TextButton(
                  onPressed: () {},
                  child: TextButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    child: const Text("关闭"),
                  ))
            ],
          );
        });
  }

  void _onTabBarClick(int index) {
    switch (index) {
      case 0:
        int curTime = DateTime.now().millisecondsSinceEpoch;
        if (curTime - lastTime > 1000) {
          lastTime = curTime;
          ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
              duration: Duration(milliseconds: 300),
              content: Text('滑动到列表开始位置')));
        }
        _homeController.animateTo(0,
            duration: const Duration(microseconds: 500), curve: Curves.easeOut);
        break;
      case 1:
        _showModal();
        break;
    }
    setState(() {
      _selectTabIndex = index;
    });
  }

  Widget _listViewBody() {
    return ListView.separated(
        padding: const EdgeInsets.all(10),
        controller: _homeController,
        itemBuilder: (context, index) {
          return Center(
            child: Text('Item $index'),
          );
        },
        separatorBuilder: (context, index) => const Divider(
              thickness: 1,
            ),
        itemCount: 50);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("BottomNavigationBarPage2"),
      ),
      body: _listViewBody(),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
          BottomNavigationBarItem(
              icon: Icon(Icons.open_in_new_rounded), label: "弹窗"),
        ],
        currentIndex: _selectTabIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onTabBarClick,
      ),
    );
  }
}
